package com.lwx.sys.controller;


import com.lwx.sys.constant.Constant;
import com.lwx.sys.domain.Permission;
import com.lwx.sys.domain.User;
import com.lwx.sys.service.PermissionService;
import com.lwx.sys.service.UserService;
import com.lwx.sys.utils.DataGridView;
import com.lwx.sys.utils.ResultObj;
import com.lwx.sys.utils.TreeNode;
import com.lwx.sys.utils.TreeNodeBuilder;
import com.lwx.sys.vo.PermissionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * 菜单相关接口
 * @author lwx
 * @since 2020-04-09
 */
@Controller
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private PermissionService permissionService;
    @Autowired
    private UserService userService;

    /**
     *  首页左侧目录树
     * @param permissionVo 权限vo
     * @param httpSession session
     * @return
     */
    @GetMapping("/loadIndexLeftMenuTreeJson")
    @ResponseBody
    public List<TreeNode> loadIndexLeftMenuTreeJson(PermissionVo permissionVo, HttpSession httpSession) {
        //查询type为menu
        permissionVo.setType(Constant.TYPE_MENU);
        //查询列表为可用的
        permissionVo.setAvailable(Constant.AVAILABLE_TRUE);
        //从session获取登录用户
        int userId = (int) httpSession.getAttribute("userId");
        User user = userService.queryUserByUserId(userId);
        //根据登录用户的用户类型显示不同的首页菜单->即不同的权限列表
        List<Permission> permissions = user.getType().equals(Constant.USER_TYPE_SUPER) ? permissionService.queryAllPermissionForList(permissionVo) : permissionService.queryPermissionByUserIdForlist(permissionVo, user.getId());
        //把permissionList变成tree格式
        ArrayList<TreeNode> treeNodes = new ArrayList<>();
        for (Permission permission : permissions) {
            Integer id = permission.getId();
            Integer pid = permission.getPid();
            String title = permission.getTitle();
            String href = permission.getHref();
            String icon = permission.getIcon();
            boolean spread = permission.getOpen() == 1;
            TreeNode treeNode = new TreeNode(id, pid, title, href, icon, spread);
            treeNodes.add(treeNode);
        }
        return TreeNodeBuilder.build(treeNodes, 1);
    }


    /**
     * 菜单管理-获取所有的菜单选项(左侧树形结构)
     * @param permissionVo
     * @return
     */
    @GetMapping("/loadAllMenuTreeJson")
    @ResponseBody
    public DataGridView loadAllMenuTreeJson(PermissionVo permissionVo){
        permissionVo.setAvailable(Constant.AVAILABLE_TRUE);
        permissionVo.setType(Constant.TYPE_MENU);
        //查询所有可用部门
        List<Permission> allPermission=permissionService.queryAllPermissionForList(permissionVo);
        List<TreeNode> treeNodes =new ArrayList<>();
        for (Permission permission:allPermission){
            Integer id = permission.getId();
            Integer pid = permission.getPid();
            String title = permission.getTitle();
            Boolean spread = permission.getOpen() == 1;
            treeNodes.add(new TreeNode(id,pid,title,spread));
        }
        return new DataGridView(treeNodes);
    }

    /**
     * 菜单管理-右侧表格所有数据
     * @param permissionVo 权限集合
     * @return
     */
    @GetMapping("/loadAllMenu")
    @ResponseBody
    public DataGridView loadAllMenu(PermissionVo permissionVo){
        permissionVo.setType(Constant.TYPE_MENU);
        return permissionService.loadAllPermission(permissionVo);
    }

    /**
     * 菜单管理-删除菜单
     * @param id
     * @return
     */
    @DeleteMapping("/deleteMenu")
    @ResponseBody
    public ResultObj deleteMenu(Integer id){
        try {
            permissionService.deletePermission(id);
            return ResultObj.DELETE_SUCESS;
        }catch (Exception e){
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 添加菜单
     * @param permission 菜单信息
     * @return
     */
    @PostMapping("/addMenu")
    @ResponseBody
    public ResultObj addMenu(Permission permission){
        try {
            permission.setType(Constant.TYPE_MENU);
            permissionService.addPermission(permission);
            return ResultObj.ADD_SUCESS;
        }catch (Exception e){
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 更新菜单信息
     * @param permission 菜单信息
     * @return
     */
    @PostMapping("updateMenu")
    @ResponseBody
    public ResultObj updateMenu(Permission permission){
        try {
            permissionService.updatePermission(permission);
            return ResultObj.ADD_SUCESS;
        }catch (Exception e){
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 检查当前菜单是否有子节点
     * @param id
     * @return
     */
    @GetMapping("/checkCurrentMenuHasChildren")
    @ResponseBody
    public DataGridView checkCurrentMenuHasChildren(Integer id){
        Integer count = permissionService.queryPermissionCountByPid(id);
        return new DataGridView(count);

    }

    /**
     * 获取菜单最大的排序号
     * @return
     */
    @GetMapping("/loadMenuMaxOrderNum")
    @ResponseBody
    public DataGridView loadMenuMaxOrderNum(){
        Integer max = permissionService.queryPermissionMaxOrderNum();
        return new DataGridView(max+1);
    }
}

